package com.ruoyi.bantong.service.impl;

import com.ruoyi.bantong.Sms.GSmsMessagesService;
import com.ruoyi.bantong.domain.*;
import com.ruoyi.bantong.mapper.*;
import com.ruoyi.bantong.service.IBtTaskService;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 版通申请Service业务层处理
 * 
 * @author laohei
 * @date 2021-01-26
 */
@Service
public class BtTaskServiceImpl implements IBtTaskService
{

    @Autowired
    private BtPlacecardPoolMapper btPlacecardPoolMapper;

    @Autowired
    private BtPlacecardPoolDetailsMapper btPlacecardPoolDetailsMapper;

    @Autowired
    private BtPlacecardApplyMapper btPlacecardApplyMapper;

    @Autowired
    private BtUserMapper btUserMapper;

    @Autowired
    private BtUserLevelMapper btUserLevelMapper;

    @Autowired
    private BtPlacecardApplyMapper placecardApplyMapper;

    @Autowired
    private IBtUserAccountRecordService accountRecordService;

    @Autowired
    private BtPlacecardConfigMapper configMapper;

    @Autowired
    private BtRedbagConfigMapper redbagConfigMapper;

    @Autowired
    private BtUserTicketMapper ticketMapper;

    @Autowired
    private GSmsMessagesService smsMessagesService;

    @Override
    @Transactional
    public void publishPlacecardPool() {
        List<Map<String, Object>> btPlacecardPools = btPlacecardPoolMapper.selectBtPlacecardPoolByPublishTime();
        for(Map<String, Object> pool : btPlacecardPools){
            BtPlacecardPoolDetails details = new BtPlacecardPoolDetails();
            details.setPpId((Integer) pool.get("pp_id"));
            Integer level = (Integer) pool.get("level");
            List<BtPlacecardPoolDetails> btPlacecardPoolDetails = btPlacecardPoolDetailsMapper.selectBtPlacecardPoolDetailsList(details);
            for(BtPlacecardPoolDetails poolDetails : btPlacecardPoolDetails){
                Integer result = poolDetails.getResult();
                if(1 == result){ //中奖
                    BtUserLevel btUserLevel = btUserLevelMapper.selectBtUserLevelByLevel(level);
                    Integer winGotNum = btUserLevel.getWinGotNum(); //中奖后获得奖券数量
                    BtPlacecardApply apply = new BtPlacecardApply();
                    BtPlacecardConfig btPlacecardConfig = configMapper.selectBtPlacecardConfigById(level);
                    apply.setOutTime(DateUtils.dateAddMinutes(null,btPlacecardConfig.getCheckInterval() * 60));
                    apply.setConvertMoney((Integer)pool.get("account_money"));
                    apply.setEarnestMoney(poolDetails.getEarnestMoney());
                    apply.setLevel((Integer)pool.get("level"));
                    apply.setPpId((Integer)pool.get("pp_id"));
                    apply.setUserId(poolDetails.getUserId());
                    apply.setStatus(-1);
                    apply.setCreateTime(DateUtils.getNowDate());
                    apply.setTicketNum(winGotNum);
                    btPlacecardApplyMapper.insertBtPlacecardApply(apply);

                    if(winGotNum > 0){
                        for (int i = 0; i < winGotNum; i++) {
                            //奖励奖券
                            BtUserTicket btUserTicket = new BtUserTicket();
                            btUserTicket.setUserId(poolDetails.getUserId());
                            btUserTicket.setTotalFee(1500);
                            btUserTicket.setCreateTime(DateUtils.getNowDate());
                            ticketMapper.insertBtUserTicket(btUserTicket);
                        }
                    }
                    BtUser btUser = btUserMapper.selectBtUserById(poolDetails.getUserId());
                    //发送短信
                    smsMessagesService.send(btUser.getPhone(), "521888");
                }else{
                    try {
                        BtUser btUser = btUserMapper.selectBtUserById(poolDetails.getUserId());
                        //退还保证金
                        accountRecordService.updataUserAccount(poolDetails.getUserId(),BigDecimal.valueOf(poolDetails.getEarnestMoney()),BigDecimal.ZERO,
                                (Integer)pool.get("pp_id"),"bt_placecard_pool",
                                "in",2);
                        //奖励一定金豆
                        BtUserLevel btUserLevel = btUserLevelMapper.selectBtUserLevelByLevel(level);
                        accountRecordService.updataUserAccount(poolDetails.getUserId(),BigDecimal.valueOf(btUserLevel.getLosGotMoney()),BigDecimal.ZERO,
                                (Integer)pool.get("pp_id"),"bt_placecard_pool",
                                "in",4);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                //修改为已公布
                BtPlacecardPool pool1 = new BtPlacecardPool();
                pool1.setPpId((Integer)pool.get("pp_id"));
                pool1.setStatus(2);
                pool1.setUpdateTime(DateUtils.getNowDate());
                btPlacecardPoolMapper.updateBtPlacecardPool(pool1);
            }
        }
    }

    @Override
    public void updatePlacecardApplyOut() {
        placecardApplyMapper.updateBtPlacecardApplyStatus2();
    }
}
